package com.wl.zlmanage.utils;/**
 * Created by Administrator on 2018/6/5.
 */

import java.io.FileOutputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;

/**
 * @program: channel
 * @description: 图片工具类
 * @author: guohailong
 * @create: 2018-06-05 15:55
 **/
public class ImageUtil {
    /**
     * 将字符串转为图片
     * @param imgStr
     * @return
     */
    public static boolean generateImage(String imgStr,String imgFile)throws Exception {// 对字节数组字符串进行Base64解码并生成图片
        // 图像数据为空
        if (imgStr == null) {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            String imgFilePath = imgFile;// 新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

//    public static void main(String[] args){
//        String imgStr = "iVBORw0KGgoAAAANSUhEUgAAABIAAAATCAYAAACdkl3yAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ\n" +
//                "bWFnZVJlYWR5ccllPAAAAyNpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdp\n" +
//                "bj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6\n" +
//                "eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTQwIDc5LjE2\n" +
//                "MDQ1MSwgMjAxNy8wNS8wNi0wMTowODoyMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJo\n" +
//                "dHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlw\n" +
//                "dGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAv\n" +
//                "IiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RS\n" +
//                "ZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpD\n" +
//                "cmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIChNYWNpbnRvc2gpIiB4bXBNTTpJbnN0YW5j\n" +
//                "ZUlEPSJ4bXAuaWlkOjAzNDgwMkU3NTY4MzExRThCOEE5QkJBQzdCRkFFRDJDIiB4bXBNTTpEb2N1\n" +
//                "bWVudElEPSJ4bXAuZGlkOjAzNDgwMkU4NTY4MzExRThCOEE5QkJBQzdCRkFFRDJDIj4gPHhtcE1N\n" +
//                "OkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6MDM0ODAyRTU1NjgzMTFFOEI4\n" +
//                "QTlCQkFDN0JGQUVEMkMiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6MDM0ODAyRTY1NjgzMTFF\n" +
//                "OEI4QTlCQkFDN0JGQUVEMkMiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94Onht\n" +
//                "cG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz6iC4yyAAAAw0lEQVR42mJkOHiPAQp4gZiFgTTwB4g/\n" +
//                "M0A12gLxUiCWZSAPPATiWEagi0BOUmSgDNxiooIhIKDGxEAlMGAGNQFxETYJUqL7PhB3APF+Sl1U\n" +
//                "DsRhQGxOiYsOAPF2IL5JahjNAeIPUPZfIC4E4iogliLVoG1A7ADEL6CGfoQahhPg8tpKIE4AYhsg\n" +
//                "/gTEM4GYg5zoZwXiJUDsB8RmQBxIKBBBee3/kE7ZeA26RQVzroEMigLi80D8jwwDQGnsHBDHAAQY\n" +
//                "AKD0JJHmE/0TAAAAAElFTkSuQmCC";
//        String path = "E:/test.png";
//        try {
//            generateImage(imgStr, path);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
